﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class OrderEventDAO : MySQLDAO, IOrderEventDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<OrderEventDTO> cmdExec;

        #endregion

        #region Contructor

        public OrderEventDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<OrderEventDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO order_event_tbl (order_id, performed_by_user_id, event_type, date_time_occurred) " +
            "VALUES (@order_id, @performed_by_user_id, @event_type, now())";
        public void Insert(OrderEventDTO dto)
        {
            dto.ID = cmdExec.PerformInsertNonQuery(insertSQL, loadParametersFromDTO(dto));
        }

        private const string selectByIDSQL =
            "SELECT * FROM order_event_tbl WHERE order_event_id = @order_event_id";
        public OrderEventDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_event_id"] = id;

            MySQLDAOCmdExecuter<OrderEventDTO>.LoadDTODelegate loadOrderEventDTO = 
                new MySQLDAOCmdExecuter<OrderEventDTO>.LoadDTODelegate(loadDTOFromReader);
            OrderEventDTO OrderEventDTO = 
                (OrderEventDTO)cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadOrderEventDTO);            

            return OrderEventDTO;
        }

        const string selectAllSQL = 
            "SELECT * FROM order_event_tbl";
        public IList<OrderEventDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<OrderEventDTO>.LoadDTODelegate loadOrderEventDTO =
                new MySQLDAOCmdExecuter<OrderEventDTO>.LoadDTODelegate(loadDTOFromReader);
            return (IList<OrderEventDTO>)cmdExec.PerformSelectQuery(selectAllSQL, null, loadOrderEventDTO);       
        }
      
        public void Update(OrderEventDTO dto)
        {
            throw new NotImplementedException();
        }

        const string deleteByIDSQL = 
            "DELETE FROM order_event_tbl WHERE order_event_id = @order_event_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_event_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(OrderEventDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_event_id"] = dto.ID;
            parameters["@order_id"] = dto.ID;
            parameters["@performed_by_user_id"] = dto.PerformedByUserID;
            parameters["@event_type"] = dto.EventType;
            parameters["@date_time_occurred"] = dto.DateTimeOccurred;
            return parameters;
        }

        private OrderEventDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            OrderEventDTO dto = new OrderEventDTO();
            dto.ID = dataReader.GetInt64("order_event_id");
            dto.OrderID = dataReader.GetInt64("order_id");
            dto.PerformedByUserID = dataReader.GetInt64("performed_by_user_id");
            dto.EventType = dataReader.GetInt32("event_type");
            dto.DateTimeOccurred = dataReader.GetDateTime("date_time_occurred");
            return dto;
        }

        #endregion
    }
}
